﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;

namespace BLL.user
{
    public class UserManager
    {
        static private String GetConnectionString()
        {
            // To avoid storing the connection String in your code, 
            // you can retrieve it from a configuration file.
            return "Data Source=WEIWANG-PC\\SQLEXPRESS;Initial Catalog=iMovie;Integrated Security=True";
        }

        public static KeyValuePair<bool, String> Login(String username, String password)
        {
            bool returnFlag = false;
            String connectionString = GetConnectionString();
            String sqlString;
            String info;

            SqlConnection sqlCon = new SqlConnection(connectionString);


            sqlString = "select * from userDetail_Tab where userDetail_name =  " + "'" + username + "'";
            SqlCommand command1 = new SqlCommand(sqlString, sqlCon);
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = command1;
            DataSet myDataSet = new DataSet();


            sqlCon.Open();

            myAdapter.SelectCommand.ExecuteNonQuery();
            sqlCon.Close();

            myAdapter.Fill(myDataSet);
            if (myDataSet.Tables[0].Rows.Count == 0)
            {
                info = "This username does not exist";
                returnFlag = false;
            }
            else
            {
                sqlString += "and userDetail_pwd =" + "'" + password + "'";

                SqlCommand command2 = new SqlCommand(sqlString, sqlCon);
                SqlDataAdapter myAdapter2 = new SqlDataAdapter();
                myAdapter2.SelectCommand = command2;
                DataSet myDataSet2 = new DataSet();

                sqlCon.Open();

                myAdapter2.SelectCommand.ExecuteNonQuery();
                sqlCon.Close();

                myAdapter2.Fill(myDataSet2);
                if (myDataSet2.Tables[0].Rows.Count == 0)
                {
                    info = "Password is wrong";
                    returnFlag = false;
                }
                else
                {
                    info = "Welcome you, " + username;
                    returnFlag = true;
                }
               
            }

            return new KeyValuePair<bool, String>(returnFlag, info);
        }
    }
}
